package com.cfec.educationplatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cfec.educationplatform.domain.TbTopic;
import com.cfec.educationplatform.service.TbTopicService;
import com.cfec.educationplatform.mapper.TbTopicMapper;
import com.cfec.educationplatform.util.ResultData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author diyu
* @description 针对表【tb_topic(拍照上传的题目信息表)】的数据库操作Service实现
* @createDate 2025-04-11 22:03:53
*/
@Service
public class TbTopicServiceImpl extends ServiceImpl<TbTopicMapper, TbTopic>
    implements TbTopicService{

    @Resource(name = "tbTopicMapper")
    private TbTopicMapper tbTopicMapper;

    @Override
    public ResultData<Object> upCameraTopic(TbTopic tbTopic) {
        // 判断数据库是否有该数据
        try {
            QueryWrapper<TbTopic> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("topic_label",tbTopic.getTopicLabel());
            TbTopic tbTopic1 = tbTopicMapper.selectOne(queryWrapper);
            // 如果没有该数据就插入数据，有就修改数据
            if (tbTopic1 == null) {
                tbTopic.setTopicState("1");
                tbTopicMapper.insert(tbTopic);
                return new ResultData<>(200,"上传成功(创建新数据)",tbTopic);
            }
            tbTopicMapper.update(tbTopic,queryWrapper);
        } catch (Exception e) {
            return new ResultData<>(500,"上传失败",e.getMessage());
        }

        return new ResultData<>(200,"上传成功(更新数据)",tbTopic);
    }
}




